package com.norns.torque.dialog

import android.content.Context
import android.os.Bundle
import android.os.IBinder
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.hi.dhl.binding.viewbind
import com.norns.torque.databinding.DialogEditNameBinding
import com.norns.torque.entrty.DeviceConfig
import com.norns.torque.utils.AppContext


class EditContentDialog : CommonDialog() {
    companion object {
        const val Target: Int = 0 //目标值
        fun newInstance(content: String, title: String, type: Int = -1): EditContentDialog {
            val args = Bundle()
            args.putString("content", content)
            args.putString("title", title)
            args.putInt("type", type)
            val fragment = EditContentDialog()
            fragment.arguments = args
            return fragment
        }
    }
    var type:Int = -1
    private val binding by viewbind<DialogEditNameBinding>()
    private var btnClickListener: ((name: String) -> Unit)? = null

    fun setClickListener(listener: (name: String) -> Unit) {
        btnClickListener = listener
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        screenWidthAspect = 0.9f
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val content = arguments?.getString("content") ?: ""
        val title = arguments?.getString("title") ?: ""
        type = arguments?.getInt("type") ?: 0
        binding.tvTitle.setText(title)
        if (type == Target) {
            binding.editName.inputType = EditorInfo.TYPE_CLASS_NUMBER
            binding.editName.hint = "请输入目标值"
        }else{
            val filters = arrayOf<InputFilter>(LengthFilter(10))
            binding.editName.setFilters(filters)
        }
        binding.editName.setText(content)
        binding.btnConfirm.setOnClickListener {
            val name = binding.editName.text?.toString() ?: ""
            if (type == Target) {
                try {
                    val intValue = Integer.parseInt(name)
                    if (intValue>DeviceConfig.range) {//大于量程
                        Toast.makeText(AppContext.context, "目标值不能大于量程", Toast.LENGTH_SHORT).show()
                    }else{
                        btnClickListener?.invoke(name)
                        dismiss()
                    }
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                    Toast.makeText(AppContext.context, "数据不合法", Toast.LENGTH_SHORT).show()
                }

            }else{
                btnClickListener?.invoke(name)
                dismiss()
            }

        }
        binding.btnCancel.setOnClickListener {
            dismiss()
        }
    }

    override fun onDestroyView() {
        hideKeyboard(binding.editName.windowToken, AppContext.context)
        binding.editName.clearFocus()
        super.onDestroyView()
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     * @param token
     */
    private fun hideKeyboard(token: IBinder?, ct: Context) {
        if (token != null) {
            val im = ct.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }
}